<?php
/**
 * @todo 登陆
 * Created by PhpStorm.
 * User: zhangbaohua
 * Date: 2018/6/19
 * Time: 下午3:03
 */

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use App\Common\lib\Util;

class LoginController extends Controller
{
    /**
     * @todo 登陆页面
     */
    public function login()
    {
        return view('login.login');
    }

    /**
     * @todo 登陆操作
     * @param string account 账号
     * @param string password 密码
     */
    public function operation(Request $request)
    {
        $url = '/api/user/dLogin';
        $content = array(
            'userName' => $request->input('account'),
            'password' => $request->input('password'),
        );
        $response = Util::toCurl($url, $content);
        $data = json_decode($response, true);
        if ( $data['code'] == '200' ) {
            Session::put('dAccessToken', $data['data']['dAccessToken']);
            Session::put('userName', $data['data']['userName']);
            if($data['data']['rank']==0){
                Session::put('rank', '普通用户');
            }else if($data['data']['rank']==1){
                Session::put('rank', '经销商');
            }else if($data['data']['rank']==2){
                Session::put('rank', '合伙人');
            }else if($data['data']['rank']==3){
                Session::put('rank', '服务中心');
            }



        }
        return $data['code'];
    }

    /**
     * @todo 登出
     * @param Request $request
     * @return int
     */
    public function logOut(Request $request)
    {
        $request->session()->forget('dAccessToken');
        $token = Session::get('dAccessToken');
        if ( !isset( $token ) ) {
            return 10;
        } else {
            return 20;
        }

    }
}